package com.ruoyi.ewem.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.ruoyi.ewem.domain.Quality;
import com.ruoyi.ewem.service.QualityService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class QualityDataListener implements ReadListener<Quality>{

    /**
     * 单次缓存的数据量
     */
    private static final int BATCH_COUNT=2;

    List<Quality> lists = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    QualityService qualityService;

    public QualityDataListener(QualityService qualityService){
        this.qualityService=qualityService;
    }

    @Override
    public void invoke(Quality quality, AnalysisContext analysisContext) {
        System.out.println("解析出的数据---------"+quality);
        lists.add(quality);
        if (lists.size()>=BATCH_COUNT){
            qualityService.qualityImport(lists);
            lists = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("解析成功-------------------");
        if (lists.size()>0){
            qualityService.qualityImport(lists);
        }
    }


}
